import { getItem } from '../../utils/storage'
import { menu } from '../../config/menu'

const pageRights = getItem('user')?.role?.pageRights || []
console.log(pageRights)

function routerReducer(state = [], action) {
    // let preState = [...state]
    console.log('执行了', action)
    switch (action.type) {
      case 'getRoutes':
        console.log('执行了', action.type)
        let list = []
        menu.forEach((menuItem => {
          if (pageRights.includes(menuItem.key)) {
            // 匹配到一级页面，说明下级页面都可以访问
            list.push(menuItem)
            // console.log('**********************************************')
            // console.log(menuItem)
          } else {
            // 匹配不到一级页面，继续匹配二级页面
            if(menuItem?.children?.length) {
              console.log('**********************************************')
              console.log(menuItem.children)
              let arr = menuItem.children.filter(cc => pageRights.includes(cc.key))
              if (arr.length > 0) {
                list.push({
                  ...menuItem,
                  // 过滤不包含权限的子页面
                  children: arr
                })
              }
            }
          }
        }))
        console.log('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
        console.log(list)
        return list;
      default:
        return state;
    }
  }

  export default routerReducer